package com.zhu.day.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.zhu.day.model.MoneyConfig;
import com.zhu.day.model.PlayMoney;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class MoneyTest {


    public PlayMoney checkStarRewards(Map<Integer, MoneyConfig> moneyConfigMap, String starid, Integer rank, long score, Integer playType) {
        PlayMoney playMoney = new PlayMoney();
        if (rank <= 0) {
            return playMoney;
        }
        if (CollectionUtils.isEmpty(moneyConfigMap)) {
            return playMoney;
        }
        MoneyConfig moneyConfig = moneyConfigMap.get(playType);
        if (Objects.isNull(moneyConfig)) {
            return playMoney;
        }
        Integer sumMoney = 0;
        Map<Integer, Integer> baseMoney = moneyConfig.getBaseMoney();
        Optional<Integer> config = ValentinePlayUtil.getConfig(baseMoney, rank);
        switch (playType) {
            case 1:
                if (!config.isPresent()) {
                    return playMoney;
                }
                sumMoney = config.get();
                playMoney.setRewards(sumMoney);
                break;
            case 2:
                Map<Integer, Integer> capped = moneyConfig.getCapped();
                //超级上限金额(不包含基础)
                Optional<Integer> capMoney = ValentinePlayUtil.getConfig(capped, rank);
                if (capMoney.isPresent()) {
                    //超级场额外奖励
                    Map<Long, Integer> superMoney = moneyConfig.getSuperMoney();
                    Optional<Integer> configScore = ValentinePlayUtil.getConfigScore(superMoney, score);
                    sumMoney += configScore.orElse(0);
                }
                //基础奖励
                if (config.isPresent()) {
                    sumMoney += config.get();
                }
                playMoney.setRewards(sumMoney);
                break;
            default:
                break;
        }
        return playMoney;
    }

    private static <K, V> Map<K, V> parseToMap(String json, Class<K> keyType, Class<V> valueType) {
        return (Map) JSON.parseObject(json, new TypeReference<Map<K, V>>(new Type[]{keyType, valueType}) {
        }, new Feature[0]);
    }


    public static void main(String[] args) {
        String json = "{\n" +
                "    \"1\":{\n" +
                "        \"baseMoney\":{\n" +
                "            \"1\":5000,\n" +
                "            \"2\":3000,\n" +
                "            \"3\":2000,\n" +
                "            \"10\":1000\n" +
                "        }\n" +
                "    },\n" +
                "    \"2\":{\n" +
                "        \"baseMoney\":{\n" +
                "            \"1\":5000,\n" +
                "            \"2\":3000,\n" +
                "            \"3\":2000,\n" +
                "            \"10\":500\n" +
                "        },\n" +
                "        \"superMoney\":{\n" +
                "            \"13140000\":5000,\n" +
                "            \"52000000\":30000\n" +
                "        },\n" +
                "        \"capped\":{\n" +
                "            \"3\":1\n" +
                "        }\n" +
                "    }\n" +
                "}";
        Map<Integer, MoneyConfig> moneyConfigMap = parseToMap(json, Integer.class, MoneyConfig.class);

        MoneyTest moneyTest = new MoneyTest();
        PlayMoney playMoney = moneyTest.checkStarRewards(moneyConfigMap, "400123", 1, 52000000l, 2);


        System.out.println(playMoney);
    }
}
